﻿using System;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;

namespace UserData
{
    public partial class Product_UserControl : UserControl
    {
        bool isAllOK = true;
        public Product_UserControl()
        {
            InitializeComponent();
        }
        public bool Func_数据对比<T>(T testData)
        {
            isAllOK = true;
            if (InvokeRequired)
            {
                this.BeginInvoke(new Action(() => { Func_数据对比(testData); }));
            }
            else
            {
                //1.获取数据值的类型
                //2.看数据能否转为double或int型，能转就比较，不能上下限就/
                //3.再看上下限能否转为double或int型,能转就比较
                PropertyInfo[] spData = testData.GetType().GetProperties();
                Type type = testData.GetType();
                dgv_ProductData.Rows.Clear();
                int row = -1;
                foreach (var sp in spData)
                {
                    //获取点检数据
                    object obj = type.GetProperty(sp.Name).GetValue(testData, null);
                    TestInfo tempSp = obj as TestInfo;
                    double d_UpLimit = 0;
                    double d_DownLimit = 0;
                    int n_UpLimit = 0;
                    int n_DownLimit = 0;

                    if (!(tempSp.Value.GetType() == typeof(double) || tempSp.Value.GetType() == typeof(int)))
                    {
                        continue;
                    }

                    dgv_ProductData.Rows.Add(1);
                    row++;
                    //判断值类型及比较结果
                    bool re = true;
                    //测试内容
                    dgv_ProductData.Rows[row].Cells[0].Value = tempSp.Name;


                    if (!double.IsNaN(tempSp.Value))
                    {
                        dgv_ProductData.Rows[row].Cells[1].Value = tempSp.Value;
                        if (tempSp.Value.GetType() == typeof(double))
                        {
                            //上限比较
                            if (double.TryParse(tempSp.Max, out d_UpLimit))
                            {
                                re &= tempSp.Value <= d_UpLimit;
                                dgv_ProductData.Rows[row].Cells[3].Value = d_UpLimit;
                            }
                            else if (int.TryParse(tempSp.Max, out n_UpLimit))
                            {
                                re &= tempSp.Value <= n_UpLimit;
                                dgv_ProductData.Rows[row].Cells[3].Value = d_UpLimit;
                            }
                            else
                            {
                                dgv_ProductData.Rows[row].Cells[3].Value = "_/";
                            }

                            //下限比较
                            if (double.TryParse(tempSp.Min, out d_DownLimit))
                            {
                                re &= tempSp.Value >= d_DownLimit;
                                dgv_ProductData.Rows[row].Cells[2].Value = d_DownLimit;
                            }
                            else if (int.TryParse(tempSp.Min, out n_DownLimit))
                            {
                                re &= tempSp.Value >= n_DownLimit;
                                dgv_ProductData.Rows[row].Cells[2].Value = n_DownLimit;
                            }
                            else
                            {
                                dgv_ProductData.Rows[row].Cells[2].Value = "_/";
                            }
                        }
                        else if (tempSp.Value.GetType() == typeof(int))
                        {
                            //上限比较
                            if (double.TryParse(tempSp.Max, out d_UpLimit))
                            {
                                re &= tempSp.Value <= d_UpLimit;
                                dgv_ProductData.Rows[row].Cells[3].Value = d_UpLimit;
                            }
                            else if (int.TryParse(tempSp.Max, out n_UpLimit))
                            {
                                re &= tempSp.Value <= n_UpLimit;
                                dgv_ProductData.Rows[row].Cells[3].Value = d_UpLimit;
                            }
                            else
                            {
                                dgv_ProductData.Rows[row].Cells[3].Value = "_/";
                            }

                            //下限比较
                            if (double.TryParse(tempSp.Min, out d_DownLimit))
                            {
                                re &= tempSp.Value >= d_DownLimit;
                                dgv_ProductData.Rows[row].Cells[2].Value = d_DownLimit;
                            }
                            else if (int.TryParse(tempSp.Min, out n_DownLimit))
                            {
                                re &= tempSp.Value >= n_DownLimit;
                                dgv_ProductData.Rows[row].Cells[2].Value = n_DownLimit;
                            }
                            else
                            {
                                dgv_ProductData.Rows[row].Cells[2].Value = "_/";
                            }
                        }
                        //else
                        //{
                        //    //结果是bool型
                        //    if (tempSp.Value.GetType() == typeof(bool) && !tempSp.Value)
                        //        re = false;
                        //    else
                        //        re = true;

                        //    dgv_ProductData.Rows[row].Cells[2].Value = "/";
                        //    dgv_ProductData.Rows[row].Cells[3].Value = "/";
                        //}
                    }
                    else
                    {
                        dgv_ProductData.Rows[row].Cells[1].Value = "N/A";
                        dgv_ProductData.Rows[row].Cells[2].Value = tempSp.Min;
                        dgv_ProductData.Rows[row].Cells[3].Value = tempSp.Max;
                    }
                    if (re)
                    {
                        dgv_ProductData.Rows[row].Cells[4].Value = "OK";
                        dgv_ProductData.Rows[row].DefaultCellStyle.BackColor = Color.White;
                        dgv_ProductData.Rows[row].Cells[4].Style.ForeColor = Color.Green;
                    }
                    else
                    {
                        dgv_ProductData.Rows[row].Cells[4].Value = "NG";
                        dgv_ProductData.Rows[row].DefaultCellStyle.BackColor = Color.LightCoral;
                        isAllOK = false;
                    }
                }
                
            }
            return isAllOK;
        }

    }
}
